<?php

namespace app\api\controller;

use app\api\logic\MpBindLogic;
use app\api\logic\WxLoginLogic;
use app\common\exception\BusinessException;
use app\common\service\GlobalsVariableService;
use think\response\Json;

class User extends Base
{
    /**
     * index
     * @return Json
     * @throws BusinessException
     */
    public function login(): Json
    {
        //外部参数
        $params = $this->request->param();

        //验证规则
        $rule = [
            'username' => 'require',
            'password' => 'require',
        ];

        //错误信息
        $errMsg = [
            'username.require' => '登录账号不得为空',
            'password.require' => '登录密码不得为空',
        ];

        //数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        //提交登录
        $result = app(\app\api\logic\User::class)->login($params);
        $response_code = $result['openid'] ? 1 : 2;

        return $this->response($result, $response_code);
    }

    /**
     * 微信登录
     * @throws BusinessException
     */
    public function wxLogin(): Json
    {
        // 外部参数
        $params = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'code.require' => '请求数据code不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交登录
        $result = app(\app\api\logic\User::class)->wxLogin($params);

        //返回
        return $this->response($result);
    }

    /**
     * 微信登录新版
     * @return Json
     * @throws BusinessException
     */
    public function wx_new_login(): Json
    {
        // 外部参数
        $params = $this->request->param();

        // 验证规则
        $rule = [
            //'iv' => 'require',
            'code' => 'require',
            //'encryptedData' => 'require',
        ];

        // 错误信息
        $errMsg = [
            //'iv.require' => '请求数据iv不得为空',
            'code.require' => '请求数据code不得为空',
            //'encryptedData.require' => '请求数据encryptedData不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交登录
        $result = app(\app\api\logic\User::class)->wxLogin($params);

        // 返回
        return $this->response($result);
    }

    /**
     * 注册
     * @throws BusinessException
     */
    public function register(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require',
            'username' => 'require|length:5,15',
            'password' => 'require|length:5,15',
        ];

        // 错误信息
        $errMsg = [
            'code.require' => '验证码不得不得为空',
            'username.require' => '登录账号不得为空',
            'username.length' => '登录账号长度最小5位,最大15位',
            'password.require' => '登陆密码不得为空',
            'password.length' => '登陆密码长度最小5位,最大15位',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 进行注册
        $result = app(\app\api\logic\User::class)->register($param);

        // 返回
        return $this->response($result);
    }

    /**
     * 重置密码
     * @throws BusinessException
     */
    public function resetPassword(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'username' => 'require',
            'password' => 'require|length:5,15',
            'verifyCode' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'username.require' => '登录账号或邮箱不得为空',
            'password.require' => '新的密码不得为空',
            'password.length' => '新的密码长度最小5位,最大15位',
            'verifyCode.require' => '邮箱验证码不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交重置
        app(\app\api\logic\User::class)->resetPassword($param);

        // 返回
        return $this->response([], 1, '重置密码成功');
    }

    /**
     * info
     * @return Json
     */
    public function info(): Json
    {
        //提取参数
        $userId = $this->request->param('userId');

        //查询用户
        $result = app(\app\api\logic\User::class)->info($userId);

        return $this->response($result);
    }

    /**
     * 更新用户信息
     * @return Json
     * @throws
     */
    public function update(): Json
    {
        //外部参数
        $param = $this->request->param();

        //提交修改
        app(\app\api\logic\User::class)->update($param);

        return $this->response([], 1, '修改成功');
    }

    /**
     * 绑定Mp
     * @return Json
     * @throws BusinessException
     */
    public function bindMp(): Json
    {
        // 外部参数
        $params = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'code.require' => '公众号验证码不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交绑定
        app(MpBindLogic::class)->bindMp($params);

        // 返回
        return $this->response();
    }

    /**
     * 绑定Openid
     * @return Json
     * @throws BusinessException
     */
    public function bindOpenid(): Json
    {
        // 外部参数
        $params = $this->request->param();

        // 验证规则
        $rule = [
            //'iv' => 'require',
            'code' => 'require',
            //'encryptedData' => 'require',
        ];

        // 错误信息
        $errMsg = [
            //'iv.require' => '请求数据iv不得为空',
            'code.require' => '请求数据code不得为空',
            //'encryptedData.require' => '请求数据encryptedData不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交绑定
        app(\app\api\logic\User::class)->bindOpenid($params);

        // 返回
        return $this->response();
    }

    /**
     * 更新邮箱地址
     * @throws BusinessException
     */
    public function changeMail(): Json
    {
        //外部参数
        $params = $this->request->param();

        //验证规则
        $rule = [
            'email' => 'require|email',
            'action' => 'require',
            'verifyCode' => 'require',
        ];

        //错误信息
        $errMsg = [
            'email.email' => '邮箱地址不正确',
            'email.require' => '邮箱地址不得为空',
            'action.require' => '请求动作不得为空',
            'verifyCode.require' => '验证码不得为空'
        ];

        //数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交处理
        app(\app\api\logic\User::class)->changeMail($params);

        //返回
        return $this->response();
    }

    /**
     * 设置登录密码
     * @throws BusinessException
     */
    public function changePassword(): Json
    {
        // 外部参数
        $params = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require',
            'password' => 'require|length:5,15',
        ];

        // 错误信息
        $errMsg = [
            'code.require' => '安全验证不通过',
            'password.require' => '登录密码不得为空',
            'password.length' => '密码长度最小5位,最大15位',
        ];

        //数据验证
        $valRel = $this->validate($params, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        //提交处理
        app(\app\api\logic\User::class)->changePassword($params);

        //返回
        return $this->response();
    }

    /**
     * 用户分享
     */
    public function share(): Json
    {
        // 请求参数
        $param = $this->request->param();
        $code = $param['code'] ?? '';
        $agentId = empty($param['agent_id']) ? 0 : $param['agent_id'];
        $requestIp = $this->request->ip();
        $appId = GlobalsVariableService::get('appId');

        // 提交处理
        if ($requestIp) {
            app(\app\api\logic\User::class)->share($appId, $code, $agentId, $requestIp);
        }

        // 返回
        return $this->response();
    }

    /**
     * 用户排行
     */
    public function ranking(): Json
    {
        // 提交查询
        $response = app(\app\api\logic\User::class)->ranking();

        // 返回
        return $this->response($response);
    }

    /**
     * 用户场景
     * @return Json
     */
    public function scene(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 提交数据
        app(\app\api\logic\User::class)->scene($param);

        // 返回
        return $this->response();
    }

    /**
     * 自动登录
     * @return void
     * @throws BusinessException
     */
    public function auth(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'authKey' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'authKey.require' => '安全验证不通过',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 提交数据
        $data = app(\app\api\logic\User::class)->auth($param);

        // 返回
        return $this->response($data);
    }

    /**
     * 切换版本
     * @return void
     */
    public function switch(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 提交数据
        $about = app(\app\api\logic\User::class)->switch($param);

        // 返回
        return $this->response($about);
    }

    /**
     * 关于我们
     */
    public function about(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 提交数据
        $about = app(\app\api\logic\User::class)->about($param);

        // 返回
        return $this->response($about);
    }

    /**
     * 退出登录
     * @return Json
     */
    public function logout(): Json
    {
        return $this->response();
    }

    /**
     * 绑定手机
     * @throws BusinessException
     */
    public function bindMobile(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require',
            'mobile' => 'require|mobile',
        ];

        // 错误信息
        $errMsg = [
            'code.require' => '验证码不得不得为空',
            'mobile.require' => '手机号码不得为空',
            'mobile.mobile' => '手机号码格式不正确',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 绑定手机
        app(\app\api\logic\User::class)->bindMobile($param);

        // 返回
        return $this->response();
    }

    /**
     * 绑定微信手机
     * @throws BusinessException
     */
    public function bindWxMobile(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'code' => 'require'
        ];

        // 错误信息
        $errMsg = [
            'wx_phone_code.require' => '手机号码不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 绑定手机
        $mobile = app(\app\api\logic\User::class)->bindWxMobile($param);

        // 返回
        return $this->response(['mobile' => $mobile]);
    }

    /**
     * 订阅消息
     */
    public function subscribe()
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'template_id' => 'require',
            'status' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'template_id.require' => '订阅模板编号不得为空',
            'status.require' => '订阅行为不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel);
        }

        // 订阅消息
        app(\app\api\logic\User::class)->subscribe($param);

        // 返回
        return $this->response();
    }
}
